﻿using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.DependencyInjection;

namespace TianShu.Office
{
    public class NullNpoiValueConvertService : INpoiValueConvertService, ISingletonDependency
    {


        public object Convert(ICell cell, Type valueType)
        {

            object value = null;
            switch (cell.CellType)
            {
                case CellType.Unknown:
                    break;
                case CellType.Numeric:
                    value = cell.NumericCellValue;
                    break;
                case CellType.String:
                    value = cell.StringCellValue??"";
                    break;
                case CellType.Formula:
                    break;
                case CellType.Blank:
                    break;
                case CellType.Boolean:
                    value = cell.BooleanCellValue;
                    break;
                case CellType.Error:
                    break;
                default:
                    break;
            }

            valueType = valueType.GenericTypeArguments.Length > 0 ? valueType.GenericTypeArguments[0] : valueType;

            if (value != null && (typeof(Enum).IsAssignableFrom(valueType)))
            {
                value = EnumExtends.GetEnumByEnumDisplayName(valueType, value?.ToString());
            }
            if (value != null && typeof(bool) == valueType && bool.TryParse(value?.ToString(), out bool result) == false)
            {
                if (value?.ToString() == "是")
                {
                    result = true;
                }
                value = result;
            }
            if (value==null)
            {
                if (typeof(string).IsAssignableFrom(valueType))
                {
                    value = "";
                }
                else if (typeof(DateTime).IsAssignableFrom(valueType))
                {
                    value = default(DateTime);
                }
                else if (typeof(decimal).IsAssignableFrom(valueType))
                {
                    value = default(decimal);
                }
                else if (typeof(double).IsAssignableFrom(valueType))
                {
                    value = default(double);
                }
                else if (typeof(float).IsAssignableFrom(valueType))
                {
                    value = default(float);
                }
                else if (typeof(int).IsAssignableFrom(valueType))
                {
                    value = default(int);
                }
                return value;
            }
            return System.Convert.ChangeType(value, valueType);
        }

        public object GetDefaultValue(Type valueType)
        {
            object value = null;
            if (typeof(string).IsAssignableFrom(valueType))
            {
                value = "";
            }
            else if (typeof(DateTime).IsAssignableFrom(valueType))
            {
                value = default(DateTime);
            }
            else if (typeof(decimal).IsAssignableFrom(valueType))
            {
                value = default(decimal);
            }
            else if (typeof(double).IsAssignableFrom(valueType))
            {
                value = default(double);
            }
            else if (typeof(float).IsAssignableFrom(valueType))
            {
                value = default(float);
            }
            else if (typeof(int).IsAssignableFrom(valueType))
            {
                value = default(int);
            }
            return value;

        }
    }
}
